
// button object
local PANEL = {};


/*------------------------------------
	initialize the hotkey editor
------------------------------------*/
function PANEL:Init( )

	// set the dialog title
	self:SetTitle( "#musex_hotkeys_title" );
	
	// set the width
	self:SetWide( 400 );
	
	// list of items for holding values
	self.Items = {};
	
	// starting positions
	local ypos = 30;
	local xpos = 0;
	
	local keylist = {};
	
	// loop through list of hotkeys
	for stroke, data in pairs( MuseX.HotKeyList ) do
	
		// copy the info over
		keylist[ data.Name ] = table.Copy( data );
		
		// store the right stroke information
		keylist[ data.Name ].UseStroke = stroke;
	
	end
	
	// total count
	local count = 0;
	
	// lowest Y position
	local lowy = 0;
	
	// loop through our sorted list of keys
	for name, data in pairs( keylist ) do
	
		// increment count
		count = count + 1;
	
		// grab the stroke
		local stroke = data.UseStroke;
	
		// create the setting label
		local label = vgui.Create( "MXLabel", self );
		
		// set the position
		label:SetPos( xpos + 8, ypos );
		
		// set the label text
		label:SetText( data.Name );
		
		// fix stuff getting cut off
		label:SetWide( label:GetWide() + 10 );
		
		// move the Y position
		local nypos = ypos + label:GetTall() + 3;
		
		// create the text entry
		local item = vgui.Create( "MXHotKeyLabel", self );
		
		// set the position
		item:SetPos( xpos + 10, nypos );
		
		// set the text
		item:ShowStroke( stroke );
		
		// set the size
		item:SetSize( 155, 20 );
		
		// create the edit button
		local edit = vgui.Create( "MXButton", self );
		// set the text
		edit:SetText( "..." );
		// setup the edit function
		edit.DoClick = function( self )
		
			MuseX.StartEditHotKey( data.Name, item, stroke );
			
		end
		
		// size & position the edit button
		edit:SetSize( 25, 16 );
		edit:SetPos( item:GetWide() + item.X + 5, nypos );
		
		if ( count % 2 == 0 ) then
		
			// move the Y position
			ypos = nypos + 30;
			
			// reset the X position
			xpos = 0;
		
		else
		
			// move the X position
			xpos = ( xpos == 200 ) && 0 || 200;
		
		end
		
		// move the lowest Y position
		lowy = nypos;
	
	end
	
	// move the Y position
	ypos = lowy + 40;
	
	// create the default button
	self.Default = vgui.Create( "MXButton", self );
	// set the text
	self.Default:SetText( "#musex_default" );
	// setup the default function
	self.Default.DoClick = function( self )
	
		// call restore function
		MuseX.DefaultHotKeys();
		
		// close
		self.GetParent( self ):Close();
		
	end
	
	// size & position default button
	self.Default:SetSize( 75, 20 );
	self.Default:SetPos( 5, ypos );
	
	
	// create the okay button
	self.Okay = vgui.Create( "MXButton", self );
	// set the text
	self.Okay:SetText( "#musex_done" );
	// setup the okay function
	self.Okay.DoClick = function( self )
		
		// close
		self.GetParent( self ):Close();
		
	end
	
	// size & position okay button
	self.Okay:SetSize( 75, 20 );
	self.Okay:SetPos( self:GetWide() - 80, ypos );
	
	// set the height of window
	self:SetTall( ypos + 30 );
	
	// custom derma hooks
	Derma_Hook( self, "Paint", "Paint", "MXMainFrame" );
	Derma_Hook( self, "ApplySchemeSettings", "Scheme", "MXFrame" );
	self.AnimIn = Derma_Anim( "OpenAnim", self, self.OpenAnim );
	self.AnimOut = Derma_Anim( "CloseAnim", self, self.CloseAnim );
	
end


/*------------------------------------
	open the manager
------------------------------------*/
function PANEL:Open( )

	// ignore if we're already visible
	if ( self:IsVisible() ) then
	
		// bye
		return;
		
	end
	
	// close menus
	CloseDermaMenus();
	
	// take focus and be visible
	self:MakePopup();
	self:SetVisible( true );
	self:DoModal();
	
	// enable keyboard input
	self:SetKeyboardInputEnabled( true );
	
	// enable mouse input
	self:SetMouseInputEnabled( true );
	
	// halt the close animation
	self.AnimOut:Stop();
	
	// start the open animation
	self.AnimIn:Start( 0.1 );

end


/*------------------------------------
	handle the opening animation
------------------------------------*/
function PANEL:OpenAnim( anim, delta, data )

	// check if it has been started
	if ( anim.Started ) then
	
		// reset alpha
		self:SetAlpha( 0 );
		
	end
	
	// check if it has been finished
	if ( anim.Finished ) then
	
		// fully opaque
		self:SetAlpha( 255 );
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 * delta );

end


/*------------------------------------
	close the manager
------------------------------------*/
function PANEL:Close( skip )
	
	// close menus
	CloseDermaMenus();
	
	// halt the opening animation
	self.AnimIn:Stop();
	
	// check if we're skipping the animation
	if ( skip ) then
	
		// restore alpha
		self:SetAlpha( 255 );
		
		// not visible
		self:SetVisible( false );
	
	else
	
		// start the close animation
		self.AnimOut:Start( 0.1 );
		
	end
	
	// cancel editing
	MuseX.FinishEditHotKey();

end


/*------------------------------------
	handle the closing animation
------------------------------------*/
function PANEL:CloseAnim( anim, delta, data )

	// check if the animation has started
	if ( anim.Started ) then
	
		// restore the alpha
		self:SetAlpha( 255 );
		
	end
	
	// check if the animation has finished
	if ( anim.Finished ) then
	
		// store alpha
		self:SetAlpha( 255 );
		
		// set as not visible
		self:SetVisible( false );
		
		// destroy
		self:Remove();
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 - 255 * delta );

end



/*------------------------------------
	manager think
------------------------------------*/
function PANEL:Think( )

	// call BaseClass
	self.BaseClass.Think( self );
	
	// run the animation functions
	self.AnimIn:Run();
	self.AnimOut:Run();

end


/*------------------------------------
	button paint
------------------------------------*/
function PANEL:Paint( )
	
	// all done
	return false;

end

// define
derma.DefineControl( "MXHotKeyEditor", "", PANEL, "DFrame" );


// panel object
PANEL = {};


/*------------------------------------
	show the stroke formatted
------------------------------------*/
function PANEL:ShowStroke( stroke )

	// table of escape strings
	local escapekeys = { "CTRL", "ALT", "SHIFT" };
	
	// list of contained escape strings
	local escape = {};
	
	// loop through the list
	for _, e in pairs( escapekeys ) do
	
		// see if our stroke contains it
		if ( stroke:find( e ) ) then
		
			// add it to the list
			escape[ #escape + 1 ] = e;
			
			// remove from stroke
			stroke = stroke:gsub( e, "" );
		
		end
	
	end
	
	// set the format text
	self:SetText( ("%s + %s"):format( stroke, table.concat( escape, " + " ) ) );

end

// define
derma.DefineControl( "MXHotKeyLabel", "", PANEL, "MXLabel" );
